package com.ddv.www.sqlcipher.dbhelper;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

import net.sqlcipher.Cursor;
import net.sqlcipher.SQLException;
import net.sqlcipher.database.SQLiteDatabase;

/**
 * Created by 黄家三少 on 2017/10/12.
 */

public class DBCipherManager {

    private DBCipherHelper dbCipherHelper;
    private volatile static DBCipherManager mInstance;

    private DBCipherManager(Context context) {
        dbCipherHelper = new DBCipherHelper(context.getApplicationContext());
    }

    public static DBCipherManager getInstance(Context context) {
        DBCipherManager instance = mInstance;
        if (instance == null) {
            synchronized (DBCipherManager.class) {
                instance = mInstance;
                if (instance == null) {
                    instance = new DBCipherManager(context);
                    mInstance = instance;
                }
            }
        }
        return instance;
    }

    /**
     * 插入数据
     *
     * @param name
     */
    public void insertData(String name, int num) {
        //获取写数据库
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        //生成要修改或者插入的键值
        ContentValues values = new ContentValues();
        values.put(DBCipherHelper.FIELD_NAME, name);
        values.put(DBCipherHelper.FIELD_NUM, num);
        // insert 操作
        db.insert(DBCipherHelper.TABLE_NAME, null, values);
        //关闭数据库
        db.close();
    }

    /**
     * 未开启事务批量插入
     *
     * @param testCount
     */
    public void insertDatasByNomarl(int testCount) {
        //获取写数据库
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        for (int i = 0; i < testCount; i++) {
            //生成要修改或者插入的键值
            ContentValues values = new ContentValues();
            values.put(DBCipherHelper.FIELD_NAME, String.valueOf(i));
            values.put(DBCipherHelper.FIELD_NUM, i);
            // insert 操作
            db.insert(DBCipherHelper.TABLE_NAME, null, values);
        }
        //关闭数据库
        db.close();
    }


    /**
     * 测试开启事务批量插入
     *
     * @param testCount
     */
    public void insertDatasByTransaction(int testCount) {
        //获取写数据库
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        db.beginTransaction();  //手动设置开始事务
        try {
            //批量处理操作
            for (int i = 0; i < testCount; i++) {
                //生成要修改或者插入的键值
                ContentValues values = new ContentValues();
                values.put(DBCipherHelper.FIELD_NAME, String.valueOf(i));
                values.put(DBCipherHelper.FIELD_NUM, i);
                // insert 操作
                db.insert(DBCipherHelper.TABLE_NAME, null, values);
            }
            db.setTransactionSuccessful(); //设置事务处理成功，不设置会自动回滚不提交
        } catch (Exception e) {

        } finally {
            db.endTransaction(); //处理完成
            //关闭数据库
            db.close();
        }
    }

    /**
     * 删除数据
     */
    public void deleteData(String name) {
        //获取写数据库
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        // delete 操作
        db.delete(DBCipherHelper.TABLE_NAME, DBCipherHelper.FIELD_NAME + "=?", new String[]{name});
        //关闭数据库
        db.close();
    }

    /**
     * 多个字段删除数据
     */
    public void deleteData(String name, int num) {
        //获取写数据库
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        // delete 操作
        db.delete(DBCipherHelper.TABLE_NAME, DBCipherHelper.FIELD_NAME + "=?and " + DBCipherHelper.FIELD_NUM + "=?",
                new String[]{name, String.valueOf(num)});
        //关闭数据库
        db.close();
    }

    /**
     * 删除所有数据
     */
    public void deleteDatas() {
        //String sql = "delete from " + DBCipherHelper.TABLE_NAME;
        //execSQL(sql);
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        db.delete(DBCipherHelper.TABLE_NAME, null, null);
        //关闭数据库
        db.close();
    }

    /**
     * 更新数据
     */
    public void updateData(String name, int num) {
        //生成要修改或者插入的键值
        ContentValues values = new ContentValues();
        values.put(DBCipherHelper.FIELD_NAME, name + name);
        values.put(DBCipherHelper.FIELD_NUM, num);
        //获取写数据库
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        /**
         * 数据的更新
         * 参数一：要更新的数据所在的表名
         * 参数二：新的数据
         * 参数三：要更新数据的查找条件
         * 参数四：条件的参数
         */
        db.update(DBCipherHelper.TABLE_NAME, values, DBCipherHelper.FIELD_NUM + "=?", new String[]{String.valueOf(num)});
        //关闭数据库
        db.close();
    }

    /**
     * 指定条件查询数据
     */
    public void queryDatas(String name) {
        //获取可读数据库
        SQLiteDatabase db = dbCipherHelper.getReadableDatabase(DBCipherHelper.DB_PASSWORD);
        //查询数据库
        Cursor cursor = null;
        try {
            cursor = db.query(DBCipherHelper.TABLE_NAME, new String[]{DBCipherHelper.FIELD_NAME},
                    DBCipherHelper.FIELD_NAME + "=?", new String[]{name}, null, null, null);
            while (cursor.moveToNext()) {
                int count = cursor.getColumnCount();
                String columName = cursor.getColumnName(0);
                String tname = cursor.getString(0);
                Log.e("huangxiaoguo", "count = " + count + " columName = " + columName + "  name =  " + tname);
            }
            if (cursor != null) {
                cursor.close();
            }
        } catch (SQLException e) {
        }
        //关闭数据库
        db.close();
    }

    /**
     * 查询全部数据
     */
    public void queryDatas() {
        //获取可读数据库
        SQLiteDatabase db = dbCipherHelper.getReadableDatabase(DBCipherHelper.DB_PASSWORD);
        //查询数据库
        Cursor cursor = null;
        try {
            //获取数据游标
            cursor = db.query(DBCipherHelper.TABLE_NAME, new String[]{DBCipherHelper.FIELD_NAME, DBCipherHelper.FIELD_NUM}, null, null, null, null, null);
            while (cursor.moveToNext()) {
                int count = cursor.getColumnCount();
                String columeName = cursor.getColumnName(0);//获取表结构列名
                String name = cursor.getString(0);//获取表结构列数据
                int num = cursor.getInt(1);
                Log.e("huangxiaoguo", "count = " + count + " columName = " + columeName + "  name =  " + name + "  num =  " + num);
            }
            //关闭游标防止内存泄漏
            if (cursor != null) {
                cursor.close();
            }
        } catch (SQLException e) {
        }
        //关闭数据库
        db.close();
    }

    /**
     * 执行sql语句
     */
    private void execSQL(String sql) {
        //获取写数据库
        SQLiteDatabase db = dbCipherHelper.getWritableDatabase(DBCipherHelper.DB_PASSWORD);
        //直接执行sql语句
        db.execSQL(sql);//或者
        //关闭数据库
        db.close();
    }

}
